package com.wyd.jwpkglxt_ssm.controller;

import com.wyd.jwpkglxt_ssm.entity.Area;
import com.wyd.jwpkglxt_ssm.entity.Role;
import com.wyd.jwpkglxt_ssm.entity.User;
import com.wyd.jwpkglxt_ssm.service.AreaService;
import com.wyd.jwpkglxt_ssm.util.ResultCode;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (Area)表控制层
 *
 * @author wyd
 * @since 2021-08-02 16:20:28
 */
@RestController
@RequestMapping("area")
public class AreaController {
    /**
     * 服务对象
     */
    @Autowired
    private AreaService areaService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Area selectOne(Integer id) {
        return this.areaService.queryById(id);
    }

    @GetMapping("area/page")
    public ResultUtil getAllAreaByPage(Integer pageNum, Integer pageSize,
                                               @RequestParam(required = false)String areaName,
                                               @RequestParam(required = false)String areaCoding) {
        Area area = new Area().setAreaName(areaName).setAreaCoding(areaCoding);
        System.out.println(area);
        return ResultUtil.ok().data("pageInfo",this.areaService.queryAllByPage(pageNum,pageSize,area));
    }

    @GetMapping("area")
    public ResultUtil getAllArea(){
        return ResultUtil.ok().data("areas",this.areaService.queryAll(null));
    }

    @PostMapping("area")
    public ResultUtil addArea(@RequestBody Area area){
        if(!Objects.isNull(area)){
            Area insertArea = this.areaService.insert(area);
            if(!Objects.isNull(insertArea)){
                return ResultUtil.ok().data("area",insertArea);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @PutMapping("area")
    public ResultUtil updateArea(@RequestBody Area area){
        System.out.println(area);
        if(!Objects.isNull(area)){
            Area updateArea = this.areaService.update(area);
            if(!Objects.isNull(updateArea)){
                return ResultUtil.ok().data("area",updateArea);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @DeleteMapping("area/{areaId}")
    public ResultUtil deleteAreaById(@PathVariable("areaId")String ids){
        if(ids.contains("-")){
            String[] idsplit = ids.split("-");
            List<Integer> areaIds = new ArrayList<>();
            for(String s : idsplit){
                areaIds.add(Integer.parseInt(s));
            }
            boolean delAreas = this.areaService.deleteAreaBatch(areaIds);
            if(delAreas){
                return ResultUtil.ok();
            }
        }else if(!"".equals(ids)){
            Integer id = Integer.parseInt(ids);
            boolean delArea = this.areaService.deleteById(id);
            if(delArea){
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }
}